package gwtI18nEditor.screens;

import gwtI18nEditor.Main;
import gwtI18nEditor.datamodel.Directory;
import gwtI18nEditor.datamodel.Message;
import gwtI18nEditor.datamodel.MessageFile;
import gwtI18nEditor.datamodel.Translation;
import gwtI18nEditor.dialogs.OpenProjectDialog;
import gwtI18nEditor.widgets.DefaultTranslationViewer;
import gwtI18nEditor.widgets.DirectoryListing;
import gwtI18nEditor.widgets.MainToolbar;
import gwtI18nEditor.widgets.MessagesListing;
import gwtI18nEditor.widgets.TranslationEditor;
import gwtI18nEditor.widgets.TranslationsListing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;

@SuppressWarnings("serial")
public class MainScreen extends JFrame implements ActionListener {

	private final DirectoryListing         directoryListing;
	private final MessagesListing          messagesListing;
	private final TranslationsListing      translationsListing;
	private final DefaultTranslationViewer defaultTranslationViewer;
	private final TranslationEditor        translationEditor;
	
	private MessageFile currentMessage = null;

	public MainScreen() {
		super("GWT-I18n-Editor");
		setLayout(new BorderLayout());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocation(10, 10);
		setSize(800, 600);
		add(new MainToolbar(this), BorderLayout.PAGE_START);
		
		JTabbedPane messageTabs = new JTabbedPane();
		
		directoryListing = new DirectoryListing() {
			@Override
			public void onSelect(MessageFile message) {
				messagesListing.updateList(message);
				currentMessage = message;
			}};
		
		JSplitPane directorySplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, 
												   directoryListing, messageTabs);
		directorySplit.setDividerLocation(200);

		add(directorySplit, BorderLayout.CENTER);
		
		messagesListing = new MessagesListing() {
			@Override
			public void onSelect(Message message) {
				translationsListing.updateList(message);
				defaultTranslationViewer.updateEditor(message);
				translationEditor.updateEditor(message);
			}};
		
		translationsListing = new TranslationsListing() {
			@Override
			public void onSelect(Translation translation) {
				translationEditor.updateEditor(translation);
			}};
		JSplitPane messagesSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
												  messagesListing, translationsListing);
		messagesSplit.setDividerLocation(290);
		
		defaultTranslationViewer = new DefaultTranslationViewer();
		translationEditor = new TranslationEditor() {
			@Override
			public void onEnter(boolean shift) {
				if (shift) messagesListing.previousMessage();
				      else messagesListing.nextMessage();
			}};
		JSplitPane defaultSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true,
				                                 defaultTranslationViewer, translationEditor);
		defaultSplit.setDividerLocation(130);
		
		JSplitPane translationSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true,
				                                     messagesSplit, defaultSplit);
		translationSplit.setDividerLocation(190);
		messageTabs.add("Messages", translationSplit);
		
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		String command = event.getActionCommand();
		if ((command==null) || command.isEmpty()) return;
		if (command.equals("Open")) {
			Main.openProject(new OpenProjectDialog().execute());
		} else if (command.equals("Refresh")) {
			Main.refreshProject();
		} else if (command.equals("Languages")) {
			
		} else if (command.equals("Add")) {
			
		} else if (command.equals("Save")) {
			if (currentMessage!=null) currentMessage.save();
		}
	}
	
	public void updateDirectoryContent(Directory root) {
		directoryListing.updateTree(root);
	}
}
